package com.nongfuspring.nongfu.serviceImpl;

import com.nongfuspring.nongfu.mapper.SystemMenuMapper;
import com.nongfuspring.nongfu.mapper.UserMapper;
import com.nongfuspring.nongfu.pojo.Menu;
import com.nongfuspring.nongfu.pojo.RouterVo;
import com.nongfuspring.nongfu.pojo.User;
import com.nongfuspring.nongfu.service.SysTemMenuService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

@Service
public class SysTemMenuServiceImpl implements SysTemMenuService {

    @Resource
    SystemMenuMapper systemMenuMapper;



    @Override
    public List<Menu> getMenuByRole(Integer userid) {

        List<Menu> menuList = systemMenuMapper.getMenuByRole(userid);

      List<Menu> returnMenus = getChildPerms(menuList,0);

      return returnMenus;

    }

    @Override
    public List<RouterVo> buildMenus(List<Menu> menuList) {
        List<RouterVo> routers = new LinkedList<RouterVo>();
        for (Menu menu : menuList) {
            RouterVo router = new RouterVo();
            router.setPath(menu.getPath());
            router.setName(menu.getName());
            router.setComponent(menu.getComponent());
            router.setIcoCls(menu.getIconCls());
            router.setHidden(false);
            List<Menu> cMenus = menu.getChildren();
            if(cMenus != null ){
                router.setChildren(buildMenus(cMenus));
            }
            routers.add(router);
        }

        return routers;
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<Menu> getChildPerms(List<Menu> list, int parentId)
    {
        List<Menu> returnList = new ArrayList<Menu>();
        for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
        {
            Menu t = (Menu) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId)
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<Menu> list, Menu t)
    {
        // 得到子节点列表
        List<Menu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Menu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Menu> getChildList(List<Menu> list, Menu t)
    {
        List<Menu> tlist = new ArrayList<Menu>();
        Iterator<Menu> it = list.iterator();
        while (it.hasNext())
        {
            Menu n = (Menu) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Menu> list, Menu t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
